<template>
  <el-container class="container">
    <!-- home界面头部 -->
    <el-header>
      <div>
        <img src="@/assets/image/heima.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- home界面左侧边栏 -->
      <el-aside :width="iscollapse? '64px' : '200px'">
        <div class="collapseBtn" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#4283CF"
          :unique-opened="true"
          :collapse="iscollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="defaultActive"
        >
          <el-submenu :index="item.id + ''" v-for="item in asideData" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="subItemClick('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- home界面右侧内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import * as allHomeFunc from '@/network/home'
export default {
  name: 'Home',
  data() {
    return {
      asideData: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      defaultActive: ''
    }
  },
  created() {
    this.getAsideDate()
    this.defaultActive = window.sessionStorage.getItem('defaultActive')
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getAsideDate() {
      const res = await allHomeFunc.getAsideDate()
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.asideData = res.data
    },
    subItemClick(defaultActive) {
      window.sessionStorage.setItem('defaultActive', defaultActive)
      this.defaultActive = defaultActive
    },
    toggleCollapse() {
      this.iscollapse = !this.iscollapse
    }
  }
}
</script>

<style lang='less' scoped>
.container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  background-color: #363d40;
  font-size: 20px;
  color: #ffffff;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #e9edf0;
}
.iconfont {
  margin-right: 10px;
}
.collapseBtn {
  width: 100%;
  height: 24px;
  line-height: 24px;
  background-color: #4a5064;
  text-align: center;
  color: #ffffff;
  font-size: 10px;
  letter-spacing: 1px;
  cursor: pointer;
}
</style>
